<!--
 * @Author: dfh
 * @Date: 2023-12-14 15:02:51
 * @LastEditors: dfh
 * @Modified By: dfh
 * @describe: 
-->
<template>
  <cw-tree :data="data" :on-load="handleLoad"></cw-tree>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { StrOrNum, TreeOptions } from '@cowain/components/tree';

//构建tree的数据
function createData() {
  return [
    {
      label: nextLabel(),
      key: 1,
      isLeaf: false,
    },
    {
      label: nextLabel(),
      key: 2,
      isLeaf: false,
    }
  ]
}

function nextLabel(currentLabel?: StrOrNum): string {
  if (!currentLabel) return "Out of Tao,One is born";
  if (currentLabel == 'Out of Tao,One is born') {
    return "Out of One,Tow";
  }
  if (currentLabel == 'Out of One,Tow') {
    return 'Out of Two, Three'
  }
  if (currentLabel == 'Out of Two, Three') {
    return 'Out of Three, the created universe'
  }
  if (currentLabel === 'Out of Three, thecreated universe') {
    return 'Out of Tao, One is born'
  }
  return '';
}

//异步加载数据
const handleLoad = (node: TreeOptions) => {
  return new Promise<TreeOptions[]>((resolve) => {
    setTimeout(() => {
      resolve([
        {
          label: nextLabel(node.label),
          key: node.key + nextLabel(node.label),
          isLeaf: false
        }
      ])
    }, 2000);
  })
}
const data = ref<TreeOptions[]>(createData());
console.log(data);
</script>

<style lang="scss" scoped></style>